<?php
namespace app\crm\model;
use think\Model;
use think\Db;
use think\Loader;
use think\Cache;

class Ordermanager extends Model
{
	/**
     * 获取订单列表
     * @param int $page <页码>
     * @param int $limit <每页显示条数>
	 * @param int $where <查询条件>
     * @return array <订单列表信息>
     */
    public function getOrderData($page,$limit,$where)
    {
		$menuname = "Order List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$username = $_SESSION["user_info"]["username"];
		
        $count = Db::name('order_info')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $dataList = Db::name('order_info')->where($where)->limit(($page-1)*$limit,$limit)->order("createtime desc")->select();
        $row_order_status = array('0'=>'未确认','1'=>'已确认','2'=>'<span style="color:#FF6600;">取消</span>','3'=>'<span style="color:#FF6600;">待审核</span>','4'=>'<span style="color:red;font-weight:bold;">退货</span>','5'=>'已分单','6'=>'部分分单','7'=>'未通过','8'=>"问题单");
		$row_pay_status = array('N'=>'未付款','M'=>'付款中','Y'=>'<span style="color:#9933CC;font-weight:bold;">已付款</span>');
		$row_shipping_status = array('0'=>'<span style="color:#FF9933;font-weight:bold;">未发货</sapn>','1'=>'已发货','2'=>'<span style="color:#0000FF;font-weight:bold;">收货确认</span>','3'=>'配货中','4'=>'已发货(部分商品)','5'=>'发货中');
		$row_shopping_name = $this->logisticsMode();
		$row_payment = array("1"=>"余额支付","2"=>"银行汇款/转帐","3"=>"货到付款");
		$row_logistics_state = array("0"=>"在途","1"=>"揽件","2"=>"疑难","3"=>"签收","4"=>"退签","5"=>"派件","6"=>"退回","7"=>"转投");
		$department = Cache::get('department');
		$users = Cache::get('users');
		$saleProgram = Cache::get('saleProgram');
		
		$i = 0;
        foreach ($dataList as &$val) {
            $val["dept_id"] = $department[$val["dept_id"]]["department_name"];
			$val["cn_name"] = $users[$val["createname"]]["cn_name"];
			$val["saleProgram"] = $saleProgram[$val["program_id"]]["program_name"];
			$order_status = $row_order_status[$val['order_status']];
			$val['order_status'] = $order_status;
			$pay_status = $row_pay_status[$val['pay_status']];
			$val['pay_status'] = $pay_status;
			
			$shopping_name = $row_shopping_name[$val['shopping_name']];
			$val['shopping_name'] = $shopping_name;
			
			$payment = $row_payment[$val['payment']];
			$val['payment'] = $payment;
			
			$shipping_status = $row_shipping_status[$val['shipping_status']];
			$val['shipping_status'] = $shipping_status;
			$val['order_status'] = $val['order_status']."  ".$val['pay_status']."   ".$val['shipping_status'];
			$val["logistics_state"] = $row_logistics_state[$val["logistics_state"]];
			$val['logistics_Info'] = str_replace('"','',$val['logistics_Info']);
			if($val["logistics_account"] && $val["logistics_state"]){
				$val["operating"] = "<a href='javascript:void(0);' onclick=\"viewLogisticsInfo("."'".$val["logistics_account"]."','".$val["logistics_state"]."','".$val["logistics_Info"]."'".")\" >查看物流</a>";
			}else{
				$val["operating"] = "";
			}
			
			//判断号码是否加密
			if($priv["encrypt_phone"] == "Y"){
				if($username != "admin"){
					$val["phone"] = substr($val["phone"],0,3)."*****".substr($val["phone"],-2);
				}
			}
			$i++;
        }
		unset($i);
		
        $rowsList = count($dataList) ? $dataList : false;
        $arrCate["code"] = 0;
        $arrCate["msg"] = '';
        $arrCate["count"] = $count;
        $arrCate["data"] = $rowsList;
        return $arrCate;
    }
	
	/**
	 * 删除订单
	 */
	public function delOrder()
	{
		$result = Db::name('order_info')->where('id',$_REQUEST["id"])->delete();
		$res = Db::name('order_goods')->where("order_id",$_REQUEST["id"])->delete();
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
	}
	
	/**
	 * 添加订单
	 * @param  [arr]  $data [订单添加信息]
	 */
	public function insertOrder($data)
    {	
		$customer_code = $data['customer_code'];
		$customerData = Db::name('vip_customer_basic')->where("customer_code = '$customer_code'")->find();
    	if (empty($customerData) && $data['task_id']) {
			Loader::model('Callplatform')->insertCustomer($data['task_id'], $data['customer_code']);
    	}
		//print_r($_REQUEST);die;
		$username = $_SESSION["user_info"]["username"];
		$d_id = $_SESSION["user_info"]["d_id"];
		$order_num = date("YmdHis").$username;
		$info = explode(",",$data['info']);
		
		foreach($info as $vm){
			$goodInfo[] = explode("-",$vm);
		}
		
		foreach($goodInfo as $v){
			$price[] = $v[3];
			$g_id[] = $v[0];
			//$g_name[] = $v[4]."-".$v[2];
			//$g_name[] = $v[0]."`*`".$v[1]."`*`".$v[2]."`*`".$v[3]."`*`".$v[4]."`*`".$v[5]."`*`".$v[6]."`*`".$v[7];  //商品id-商品数量-商品单价-商品总价格-商品名称-商品货号-商品分类-商品品牌
			$g_name[] = array(
				"id"=>$v[0],			//商品id
				"goods_num"=>$v[1],		//商品数量
				"price"=>$v[2],			//商品单价
				"goods_money"=>$v[3],	//商品总价格
				"good_name"=>$v[4],		//商品名称
				"good_num"=>$v[5],		//商品货号
				"cat_name"=>$v[6],		//商品分类
				///"brand_name"=>$v[7],	//商品品牌
			);
			$nums[] = $v[1];
			$g_num[] = $v[4]."[".$v[5]."]*".$v[1];  //商品名称[商品货号]*商品数量
		}
		$goods_total = array_sum($nums);
		$cope_money = array_sum($price);
		$goods_id = implode(",",$g_id);
		//$goods_name = implode(",",$g_name);
		$goods_name = json_encode($g_name);
		$order_goods_num = implode(",",$g_num);
		//$phone = $data['phone'];
		//$telephone = $data['telephone'];
		$phoneArr = Db::name('vip_customer_basic')->where("customer_code = '$customer_code'")->field("phone,telephone")->find();
		$order_count = Db::name('order_info')->where("customer_code = '$customer_code' AND order_status = '1'")->count();
		//$order_type = $_REQUEST["order_type"]
		
		$arrData = array(
			"order_num"=>$order_num,
			"goods_id"=>$goods_id,
			"goods_total"=>$goods_total,
			"goods_name"=>$goods_name,
			"customer_code"=>$customer_code,
			"consignee"=>$data['consignee'],
			"createname"=>$username,
			"goods_amount"=>$data["goods_amount"],
			"dept_id"=>$d_id,
			"createtime"=>date("Y-m-d H:i:s"),
			"order_status"=>'3',           //状态
			"pay_status"=>'N',
			"shipping_status"=>'0',
			"province"=>$data['province'],
			"city"=>$data['city'],
			"district"=>$data['area'],
			"delivery_address"=>$data['delivery_address'],
			"telephone"=>$phoneArr["telephone"],
			"phone"=>$phoneArr["phone"],
			"program_id"=>$data["program_id"],
			"order_goods_num"=>$order_goods_num,
			"modify_price"=>empty($data['modify_price']) ? "N" : $data['modify_price'],
			"import_mark"=>$data["import_mark"],
			"task_id"=>$data["task_id"],
			"description"=>$data["description"],
		);
		// print_r($arrData);exit;

		$processCache = Cache::get('processCache');
		if($data['modify_price']=="Y"){
			$arrData["audit_status"] = "T";
			$arrData["step_num"] = 1;
			$arrData["authorized_work_num"] = $processCache[1]["authorized_work_num"];
			$arrData["cope_money"]=$data["modify_money"];
		}else{
			$arrData["cope_money"]=$data["goods_amount"];
		}
		
		// 过数据任务 实际开单
		$this->orderDataTask($customer_code, $arrData["cope_money"]);
		$result = Db::name('order_info')->insert($arrData);
		if($result){
			$order_id = Db::name('order_info')->getLastInsID();
			$sql = "insert into xy_order_goods(goods_id,goods_num,goods_price,goods_money,order_id) values";
			$value = "";
			foreach($goodInfo as $row){
				$str = "(";
				$str .= "'" .$row[0]. "',";  	 //商品id
				$str .= "'" .$row[1]. "',";  	 //商品数量
				$str .= "'" .$row[2]. "',";  	 //商品价格
				$str .= "'" .$row[3]. "',";  	 //商品总价格
				$str .= "'" .$order_id. "'";	 //订单id---最后一个不需要","
				
				$str .= ")";
				$value .= empty($value)?"$str":",$str";
			}
			if( $value ){
				$sql .= $value;
				//dump($sql);die;
				$res = Db::execute($sql);
			}
			
			
			//添加订单时，添加一条跟踪信息,记录录音
			$arrF = array(
				"order_id" => $order_id,
				"action_time" => date("Y-m-d H:i:s"),
				"action_user" => $username,
				"order_status" => "T",
				"pay_status" => "N",
				"shipping_status" => "0",
				"action_description" => "添加订单"
			);
			$res = Db::name('order_action')->insert($arrF);
			//给客户资料更新最后下单时间
			$cus = Db::name('vip_customer_detail')->where("customer_code = '$customer_code'")->update(['last_order_time' => date("Y-m-d H:i:s")]);
			Db::name('vip_customer_detail')->where("customer_code = '$customer_code'")->setInc('order_num');
			// echo $cus;exit;
		}
		if ($result){
            echo json_encode(array('success'=>true,'msg'=>'添加成功！'));
        } else {
            echo json_encode(array('msg'=>'添加失败！'));
        }
	}


	/**
	 * 数据任务订单
	 * @param  string $customer_code 客户编码
	 * @param  string $order_price   订单实际价格
	 */
	protected function orderDataTask ($customer_code, $order_price)
	{
		$res = Db::name('vip_data_customer')->field('id, order_time, order_price')->where('customer_code', $customer_code)->order('addtime DESC')->find();
		// print_r($res);exit;
		if (empty($res['order_time']) || empty($res['order_price'])) {
			$result = Db::name('vip_data_customer')->where('id', $res['id'])->update(['order_time'=>date('Y-m-d H:i:s'), 'order_price'=>$order_price]);
		}
	}




	/**
	 * 根据订单id获取订单信息
	 * @param  [int]  $order_num  [订单order_num]
	 * @return [arr]  $arrData    [订单详情]
	 */
	public function getOrderDetail($order_num)
	{
		$fields = "id,consignee,createtime,phone,telephone,delivery_address,modify_price,cope_money,order_status,pay_status,shipping_status,delivery_order,payment,customer_code,order_num,createname,step_num,audit_status,description";
		$arrData = Db::name('order_info')->field($fields)->where('order_num',$order_num)->find();
        return $arrData;
	}
	
	/**
	 * 根据id获取订单里的商品
	 * @param  [int]  $id        [订单id]
	 * @return [arr]  $arrCate   [订单商品信息]
	 */
	public function getOrderGoods($id)
	{
		$count = Db::name('order_goods')->where("order_id = $id")->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $dataList = Db::name('order_goods')->where("order_id = $id")->select();
        $goodsInfo = Cache::get('goodsInfo');
		foreach($dataList as &$val){
			$val["good_name"] = $goodsInfo[$val["goods_id"]]["good_name"];
			$val["good_num"] = $goodsInfo[$val["goods_id"]]["good_num"];
			$val["price"] = $val["goods_price"];
			$val["cat_name"] = $goodsInfo[$val["goods_id"]]["cat_name"];
			$val["simple_description"] = $goodsInfo[$val["goods_id"]]["simple_description"];
		}
        $rowsList = count($dataList) ? $dataList : false;
        $arrCate["code"] = 0;
        $arrCate["msg"] = '';
        $arrCate["count"] = $count;
        $arrCate["data"] = $rowsList;
        return $arrCate;
	}
	
	/**
	 *保存状态 
	 */
	public function updateStatus()
	{
		$username = $_SESSION["user_info"]["username"];
		//$para_sys = readS();
		$id = $_REQUEST['id'];
		$customer_code = $_REQUEST['customer_code'];
		$createname = $_REQUEST['createname'];
		$order_num = $_REQUEST['order_num2'];  //订单号
		$status = $_REQUEST['status'];
		$order = strpos($status,"order");
		$pay = strpos($status,"pay");
		$ship = strpos($status,"ship");
		//print_r($_REQUEST);die;
		if($order){
			if($status == "confirm_order"){   //确认订单
				$arrData = array(
					"order_status"=>"1",   //已确认
					"members_bomb"=>$username
				);
			}elseif($status == "confirm_todelivery_order"){   //确认订单-直接发货
				$arrData = array(
					"order_status"=>"1",  
					"payment"=>"3",
					"pay_status"=>"N",   //已付款
					"paytime"=>date("Y-m-d H:i:s"),
					"shipping_status"=>'3',   //配货中
					"delivery_order"=>"00".date("YmdHis"),
					"shopping_name"=>$_REQUEST['shopping_name'],  //配送方式
					"members_bomb"=>$username
				);
			}elseif($status == "not_order"){   //待审核
				$arrData = array(
					"order_status"=>"3",   //待审核
					"shipping_status"=>"0",  //未发货
				);
			}elseif($status == "cancel_order"){    //取消
				$arrData = array(
					"order_status"=>"2",   //取消
					"pay_status"=>"N",     //未付款
				);
			}elseif($status == "reset_order"){   //退货
				$arrData = array(
					"order_status"=>"4",   //退货
					"pay_status"=>"N",  //未付款
					"shipping_status"=>"0",  //未发货
				);
			}elseif($status == "confirm_notby_order"){   //未通过
				$arrData = array(
					"order_status"=>"7",   //未通过
					"members_bomb"=>$username
				);
			}elseif($status == "confirm_suspected_order"){   //问题单
				$arrData = array(
					"order_status"=>"8",   //问题单
					"members_bomb"=>$username
				);
			}else{
				$arrData = array(
					"order_status"=>"0",
				);
			}
			$result = Db::name('order_info')->where("id = '$id'")->update($arrData);
			if ($result !== false){
				$this->saveTrack($id,$_REQUEST['description']);
				//积分--如果有快递100推送的就改成在签收之后，否者就确定订单之后
				/*if(!$para_sys["express_key"]){
					$this->saveIntegral($customer_id);
				}*/
				echo json_encode(array('success'=>true,'msg'=>'订单状态已改变！'));
			}else{
				echo json_encode(array('msg'=>'订单状态改变失败！'));
			}
		}
		if($pay){
			if($status == "confirm_pay"){    //付款
				$arrData = array(
					"payment"=>$_REQUEST['payment'],
					"pay_status"=>"Y",   //已付款
					"paytime"=>date("Y-m-d H:i:s"),
				);
			}elseif($status == "confirm_pay_mod"){    //付款方式
				$arrData = array(
					"payment"=>$_REQUEST['payment'],
					"pay_status"=>"N",   //已付款
					"paytime"=>date("Y-m-d H:i:s"),
				);
			}elseif($status == "not_pay"){   //设为未付款
				$arrData = array(
					"pay_status"=>"N",     //未付款
					"order_status"=>"1",   //已确认
					"shipping_status"=>"0",  //未发货
					"payment"=>"",  //付款方式
				);
			}else{
				$arrData = array(
					"pay_status"=>"M",
				);
			}
			$result = Db::name('order_info')->where("id = '$id'")->update($arrData);
			if ($result !== false){
				$this->saveTrack($id,$_REQUEST['description']);
				echo json_encode(array('success'=>true,'msg'=>'付款状态已改变！'));
			}else{
				echo json_encode(array('msg'=>'付款状态改变失败！'));
			}
		}
		if($ship){
			if($status == "picking_ship"){    //配货
				$arrData = array(
					"shipping_status"=>'3',    //配货中
				);
			}elseif($status == "delivery_ship"){   //生成发货单
				$arrData = array(
					"shipping_status"=>'3',   //配货中
					"delivery_order"=>"00".date("YmdHis"),
				);
			}elseif($status == "confirm_ship"){   //发货
				$arrData = array(
					"logistics_account"=>$_REQUEST['logistics_account'],   //物流号
					"shipping_status"=>'1',   //已发货
					"deliverytime"=>date("Y-m-d H:i:s")
				);
			}elseif($status == "receipt_ship"){   //已收货
				$arrData = array(
					"shipping_status"=>'2',   //收货确认
					"receipttime"=>date("Y-m-d H:i:s"),
				);
			}elseif($status == "not_ship"){   //未发货
				$arrData = array(
					"shipping_status"=>'0',   //未发货
				);
			}else{
				$arrData = array(
					"shipping_status"=>'0',  //未发货
				);
			}
			$result = Db::name('order_info')->where("id = '$id'")->update($arrData);
			
			if ($result !== false){
				$this->saveTrack($id,$_REQUEST['description']);
				echo json_encode(array('success'=>true,'msg'=>'发货状态已改变！'));
			}else{
				echo json_encode(array('msg'=>'发货状态改变失败！'));
			}
		}
	
	}
	
	
	//用户积分
	public function saveIntegral($customer_code){
		$arrF = Db::name('order_info')->field("sum(goods_amount) as goods_amount,id")->where("customer_code = '$customer_code' AND order_status = '1'")->find();
		$integral = $arrF["goods_amount"];
		if(!$integral || $integral == "0.00"){
			$integral = "0";
		}
		$arrML = Db::name('vip_membership_level')->where(" start_num <= '$integral' AND end_num >= '$integral'")->find();
		$groups = $arrML["groups"];
		if(!$arrML){
			if(!$integral){
				$groups = "failure";
			}else{
				$arrML2 = $membership_level->where("end_num = '0'")->find();
				$groups = $arrML2["groups"];
			}
		}
		
		$arrData = array(
			"integral"=>$integral,
			"member_Level"=>$groups
		);
		$res = Db::name('vip_customer_detail')->data($arrData)->where("customer_code = '$customer_code'")->save();
		return true;
	}
   
   	/**
   	 * 获取物流方式 
   	 */
	public function logisticsMode(){
		$arrData = Db::name('order_logistics_mode')->order("id asc")->field("id,logistics_name as name")->select();
		foreach($arrData as $val){
			$arrF[$val["id"]] = $val["name"];
		}
		return $arrF;
	}
	
	/**
   	 * 获取界面物流方式 
   	 */
	public function getLogisticsMode(){
		$arrData = Db::name('order_logistics_mode')->order("id asc")->field("id,logistics_name as name")->select();
		return $arrData;
	}
	

	/**
	 * 记录订单操作动态
	 * @param  [int]  $id          [description]
	 * @param  [str]  $description [description]
	 * @return [type]              [description]
	 */
	public function saveTrack($id,$description){
		$username = $_SESSION["user_info"]["username"];
		$arrD = Db::name("order_info")->field("customer_code,order_status,pay_status,shipping_status")->where("id = '$id'")->find();
		$customer_code = $arrD["customer_code"];
		$arrF = array(
			"order_id" => $id,
			"action_time" => date("Y-m-d H:i:s"),
			"action_user" => $username,
			"order_status" => $arrD["order_status"],
			"pay_status" => $arrD["pay_status"],
			"shipping_status" => $arrD["shipping_status"],
			"action_description" => $description,
			"uniqueid"=>$_SESSION[$username."_order"][$customer_code],
		);
		$res = Db::name("order_action")->insert($arrF);
	}
	
	//价格审核列表
	public function checkPriceDataList()
	{
		$username = $_SESSION["user_info"]["username"];
		$page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
		
		//需先判断传值是否为空
        $where = "(1 AND modify_price = 'Y' AND find_in_set('$username',authorized_work_num) AND audit_status != 'Y'";
        $where .= empty($_REQUEST["name"])?"":" AND consignee like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND createname = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone = '".$_REQUEST["phone"]."'";
		$where .= empty($_REQUEST["start_createtime"])?"":" AND createtime >= '".$_REQUEST["start_createtime"]."'";
		$where .= empty($_REQUEST["dept_id"])?"":" AND dept_id = '".$_REQUEST["dept_id"]."'";
		$where .= empty($_REQUEST["end_createtime"])?"":" AND createtime <= '".$_REQUEST["end_createtime"]."'";
        $where .= " )";
		
		$count = Db::name('order_info')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $dataList = Db::name('order_info')->where($where)->limit(($page-1)*$limit,$limit)->order("createtime desc")->select();
		//echo Db::name('order_info')->getLastSql();die;
        $row_audit_status = array('T'=>'<span style="color:orange;font-weight:bold;">审核中</span>','Y'=>'<span style="color:green;font-weight:bold;">审核通过</span>','N'=>'<span style="color:red;font-weight:bold;">未通过</span>'); 
		$department = Cache::get('department');
		$users = Cache::get('users');
		$i = 0;
        foreach ($dataList as &$val) {
            $val["dept_id"] = $department[$val["dept_id"]]["department_name"];
			$val["cn_name"] = $users[$val["createname"]]["cn_name"];
			
			$audit_status = $row_audit_status[$val['audit_status']];
			$val['audit_status'] = $audit_status;
			$i++;
        }
		unset($i);
        $rowsList = count($dataList) ? $dataList : false;
        $arrCate["code"] = 0;
        $arrCate["msg"] = '';
        $arrCate["count"] = $count;
        $arrCate["data"] = $rowsList;
        return $arrCate;
	}
	
	//审核价格
	public function checkPrice()
	{
		$audit_status = $_REQUEST["status"];
		$step_num = $_REQUEST["step_num"];
		$order_num = $_REQUEST["order_num2"];
		$id = $_REQUEST["order_id"];
		$processCache = Cache::get('processCache');
		//$description = $_REQUEST["description"];
		if($step_num == "1"){
			if($audit_status == "N"){
				$arrData = array(
					"audit_status" => "N"
				);
			}else{
				$arrData = array(
					"step_num" => "2",
					"authorized_work_num" => $processCache[2]["authorized_work_num"]
				);
			}
		}elseif($step_num == "2"){
			$arrData = array(
				"audit_status" => $audit_status
			);
		}
		$result = Db::name('order_info')->where("order_num = '$order_num'")->update($arrData);
		if ($result !== false){
			$this->saveTrack($id,$_REQUEST['description']);
			echo json_encode(array('success'=>true,'msg'=>'审核成功！'));
		}else{
			echo json_encode(array('msg'=>'审核失败！'));
		}
	}
	
	//批量修改物流信息
	public function updateLogistics(){
		$id = $_REQUEST['id'];
		$arrData = array(
			'shopping_name'=>$_REQUEST['shopping_name'],
			'logistics_account'=>$_REQUEST['logistics_account'],
			'shipping_status'=>$_REQUEST['shipping_status']
		);
		$result = Db::name('order_info')->where("id in ($id)")->update($arrData);
		
		if ($result !== false){
			echo json_encode(array('success'=>true,'msg'=>"更新成功！"));
		} else {
			echo json_encode(array('msg'=>'更新失败！'));
		}
	}
	
	//打印货单
	public function printInvoiceList()
	{
		$page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
		
		//需先判断传值是否为空
        $where = "(1 AND (modify_price = 'N' OR (modify_price = 'Y' AND audit_status = 'Y') )";
        $where .= empty($_REQUEST["name"])?"":" AND consignee like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND createname = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone = '".$_REQUEST["phone"]."'";
		$where .= empty($_REQUEST["start_createtime"])?"":" AND createtime >= '".$_REQUEST["start_createtime"]."'";
		$where .= empty($_REQUEST["dept_id"])?"":" AND dept_id = '".$_REQUEST["dept_id"]."'";
		$where .= empty($_REQUEST["end_createtime"])?"":" AND createtime <= '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND customer_code = '".$_REQUEST["customer_code"]."'";
		$where .= empty($_REQUEST["order_status"])?"":" AND order_status = '".$_REQUEST["order_status"]."'";
		$where .= empty($_REQUEST["pay_status"])?"":" AND pay_status = '".$_REQUEST["pay_status"]."'";
		$where .= empty($_REQUEST["shipping_status"])?"":" AND shipping_status = '".$_REQUEST["shipping_status"]."'";
		$where .= empty($_REQUEST["logistics_account"])?"":" AND logistics_account = '".$_REQUEST["logistics_account"]."'";
		$where .= empty($_REQUEST["logistics_state"])?"":" AND logistics_state = '".$_REQUEST["logistics_state"]."'";
		$where .= empty($_REQUEST["shopping_name"])?"":" AND shopping_name = '".$_REQUEST["shopping_name"]."'";
		$where .= empty($_REQUEST["order_num"])?"":" AND order_num = '".$_REQUEST["order_num"]."'";
        $where .= " )";
		
		$arrData = Db::name('order_info')->order("createtime asc")->where($where)->select();
		
		$users = Cache::get('users');
		$row_logistics_state = array("0"=>"在途","1"=>"揽件","2"=>"疑难","3"=>"签收","4"=>"退签","5"=>"派件","6"=>"退回","7"=>"转投");
		$i = 1;
		foreach($arrData as &$val){
			$val["logistics_state"] = $row_logistics_state[$val["logistics_state"]];
			$val["cn_name"] = $users[$val["createname"]]["cn_name"];
			if($val["cn_name"]){
				$val["createname"] = $val["createname"]."/".$val["cn_name"];
			}
			$val["goods_name"] = json_decode($val["goods_name"],true);
			$val["shop_num"] = count($val["goods_name"]);
			
			$val["mod"] = $i%2;
			$i++;
		}
		
		return $arrData;
	}
	
	//打印顺丰单
	public function printSFList()
	{
		$page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
		
		//需先判断传值是否为空
        $where = "(1 AND (modify_price = 'N' OR (modify_price = 'Y' AND audit_status = 'Y') )";
        $where .= empty($_REQUEST["name"])?"":" AND consignee like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND createname = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone = '".$_REQUEST["phone"]."'";
		$where .= empty($_REQUEST["start_createtime"])?"":" AND createtime >= '".$_REQUEST["start_createtime"]."'";
		$where .= empty($_REQUEST["dept_id"])?"":" AND dept_id = '".$_REQUEST["dept_id"]."'";
		$where .= empty($_REQUEST["end_createtime"])?"":" AND createtime <= '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND customer_code = '".$_REQUEST["customer_code"]."'";
		$where .= empty($_REQUEST["order_status"])?"":" AND order_status = '".$_REQUEST["order_status"]."'";
		$where .= empty($_REQUEST["pay_status"])?"":" AND pay_status = '".$_REQUEST["pay_status"]."'";
		$where .= empty($_REQUEST["shipping_status"])?"":" AND shipping_status = '".$_REQUEST["shipping_status"]."'";
		$where .= empty($_REQUEST["logistics_account"])?"":" AND logistics_account = '".$_REQUEST["logistics_account"]."'";
		$where .= empty($_REQUEST["logistics_state"])?"":" AND logistics_state = '".$_REQUEST["logistics_state"]."'";
		$where .= empty($_REQUEST["shopping_name"])?"":" AND shopping_name = '".$_REQUEST["shopping_name"]."'";
		$where .= empty($_REQUEST["order_num"])?"":" AND order_num = '".$_REQUEST["order_num"]."'";
        $where .= " )";
		
		$arrData = Db::name('order_info')->order("createtime asc")->where($where)->select();
		
		$users = Cache::get('users');
		$row_logistics_state = array("0"=>"在途","1"=>"揽件","2"=>"疑难","3"=>"签收","4"=>"退签","5"=>"派件","6"=>"退回","7"=>"转投");
		$i = 1;
		foreach($arrData as &$val){
			$val["logistics_state"] = $row_logistics_state[$val["logistics_state"]];
			$val["cn_name"] = $users[$val["createname"]]["cn_name"];
			if($val["cn_name"]){
				$val["createname"] = $val["createname"]."/".$val["cn_name"];
			}
			$val["goods_name"] = json_decode($val["goods_name"],true);
			$val["shop_num"] = count($val["goods_name"]);
			
			$val["mod"] = $i%2;
			$i++;
		}
		
		return $arrData;
	}

}

?>

